// pages/commitem/index.js
Page({
    data: {
        cid: 0,
        comm: []
    },
    onLoad: function (options) {
        let cid = options.cid;
        this.setData({
            cid
        });
        wx.request({
            // 查找该商品
            url: 'http://460d80b632.zicp.vip/WeChat/commitem.php',
            method: "POST",
            header: {
                "Content-Type": "application/x-www-form-urlencoded"
            },
            data: {
                "cid": cid
            },
            success: (result) => {
                let comm = result.data;
                this.setData({
                    comm
                });
            },
            fail: (res) => {},
            complete: (res) => {},
        });

        wx.getStorage({
            key: "user",
            success: res => {
                let uid = res.data.uid;
                wx.request({
                    // 历史+1
                    url: 'http://460d80b632.zicp.vip/WeChat/history.php',
                    method: "POST",
                    header: {
                        "Content-Type": "application/x-www-form-urlencoded"
                    },
                    data: {
                        "uid": uid
                    },
                    success: (result) => {}
                });
            }
        })
    },
    inputCat: function (res) {
        wx.getStorage({
            key: "user",
            success: res => {
                let uid = res.data.uid;
                wx.request({
                    // 添加购物车
                    url: 'http://460d80b632.zicp.vip/WeChat/doCartAdd.php',
                    method: "POST",
                    header: {
                        "Content-Type": "application/x-www-form-urlencoded"
                    },
                    data: {
                        "cid": this.data.cid,
                        "uid": uid
                    },
                    success: (result) => {
                        if (result.data.success) {
                            wx.showToast({
                                title: '添加成功！',
                                icon: "success",
                                mask: true,
                                success: (res) => {},
                                fail: (res) => {},
                                complete: (res) => {},
                            })
                        }
                    }
                });
            },
            fail: res => {
                this.goToLogin()
            }
        })
    },
    inputBuy: function (res) {
        wx.getStorage({
            key: "user",
            success: res => {
                let uid = res.data.uid;
                wx.request({
                    // 添加订单
                    url: 'http://460d80b632.zicp.vip/WeChat/doOrderAdd.php',
                    method: "POST",
                    header: {
                        "Content-Type": "application/x-www-form-urlencoded"
                    },
                    data: {
                        "cid": this.data.cid,
                        "uid": uid
                    },
                    success: (result) => {
                        if (result.data.success) {
                            wx.showToast({
                                title: '购买成功！',
                                icon: "success",
                                mask: true,
                                success: (res) => {},
                                fail: (res) => {},
                                complete: (res) => {},
                            })
                        }
                    }
                });
            },
            fail: res => {
                this.goToLogin()
            }
        });
    },
    goToLogin: function () {
        wx.showModal({
            cancelColor: '#ff0000',
            cancelText: '取消',
            confirmColor: '#00ff00',
            confirmText: '去登陆',
            content: '您还未登陆',
            placeholderText: 'placeholderText',
            showCancel: true,
            title: '提示',
            success: (result) => {
                if (result.confirm) {
                    wx.reLaunch({
                        url: '/pages/UserLogin/index',
                        success: (res) => {},
                        fail: (res) => {},
                        complete: (res) => {},
                    });
                }
            },
            fail: (res) => {},
            complete: (res) => {},
        });
    },
    collection: function () {
        wx.getStorage({
            key: "user",
            success: res => {
                let uid = res.data.uid;
                wx.request({
                    // 收藏数
                    url: 'http://460d80b632.zicp.vip/WeChat/collection.php',
                    method: "POST",
                    header: {
                        "Content-Type": "application/x-www-form-urlencoded"
                    },
                    data: {
                        "uid": uid
                    },
                    success: (result) => {
                        if (result.data.success) {
                            wx.showToast({
                                title: '收藏成功！',
                                icon: "success",
                                mask: true,
                                success: (res) => {},
                                fail: (res) => {},
                                complete: (res) => {},
                            })
                        }
                    }
                });
            },
            fail: res => {
                this.goToLogin()
            }
        });
    }
})